<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.qiqianyi.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\company\controller;

use app\admin\model\RouteModel;
use app\company\model\CompanyCategoryModel;
use cmf\controller\AdminBaseController;
use app\company\model\MemberModel;
use think\Db;
use app\company\service\CategoryService;
use app\company\service\CompanyService;
use app\common\model\DistrictModel;
use app\company\model\CompanyModel;
use app\user\model\UserModel;
use app\company\model\QaModel;
use app\company\model\QaCategoryModel;



class AdminQaController extends AdminBaseController
{
    public function index(){




        $qaModel=   new QaModel ();

        $param = $this->request->param();


        $where = [
            'delete_time'    => 0,
            'parent_id'    => 0,
        ];

        if(!empty($param['type_id'])){
            $where['type_id'] =  $param['type_id'];
        }

        if(!empty($param['status_id'])){
            $where['status_id'] =  $param['status_id'];
        }

        if(!empty($param['title'])){
            $where['title'] = ['like', "%".$param['title']."%"];
        }




        $qsList        = $qaModel->alias('a')->where($where) ->order('id', 'DESC') ->paginate(10);

        //echo $qaModel->getLastSql();
        $qsList->appends($param);

        $this->assign('start_time', isset($param['start_time']) ? $param['start_time'] : '');
        $this->assign('end_time', isset($param['end_time']) ? $param['end_time'] : '');
        $this->assign('keyword', isset($param['keyword']) ? $param['keyword'] : '');
        $this->assign('qa_list', $qsList->items());
        $this->assign('page', $qsList->render());


        $qaCategoryList= CategoryService::queryQaCategoryList();
        $this->assign('qa_category', $qaCategoryList);


        $qaStatusList= CategoryService::queryQaStatusList();
        $this->assign('qa_status', $qaStatusList);

        return $this->fetch();
    }

    public function add(){

        $qaStatusList = CategoryService::queryQaStatusList();
        $this->assign('qa_status', $qaStatusList);

        $qaCategoryList= CategoryService::queryQaCategoryList();
        $this->assign('qa_category', $qaCategoryList);

        $memberGroupList = CategoryService::queryMemberGroupList();
        $this->assign('member_group', $memberGroupList);


        //echo "wxl:" . cmf_get_current_user_id();

        $companyModel =new CompanyModel ();


        $where = [
            'delete_time'    => 0
        ];

        $order    = "list_order";

        $companyList = $companyModel->where($where)->order($order)->select();

        $this->assign('company_list', $companyList);

        return $this->fetch();
    }


    public function addPost()
    {


            if ($this->request->isPost()) {
                $data   = $this->request->param();
                $post   = $data['post'];

                $data['post']["create_time"] =  time();
                $data['post']["update_time"] =  time();


                if (!empty($data['file_names']) && !empty($data['file_urls'])) {
                    $data['post']['more']['files'] = [];
                    foreach ($data['file_urls'] as $key => $url) {
                        $fileUrl = cmf_asset_relative_url($url);
                        array_push($data['post']['more']['files'], ["url" => $fileUrl, "name" => $data['file_names'][$key]]);
                    }
                }



                if (!empty($data['post']['more']['thumbnail'])) {
                    $data['post']['more']['thumbnail'] = cmf_asset_relative_url($data['post']['more']['thumbnail']);
                    $data['post']['thumbnail']         = $data['post']['more']['thumbnail'];
                }



                $result = $this->validate($post, 'Qa');
                    if ($result !== true) {
                        $this->error($result);
                    } else {
                        $qaModel = new QaModel ();
                        $qaModel->allowField(true)->data($data['post'], true)->isUpdate(false)->save();

                        if ($result !== false) {

                            $this->success("添加成功！", url("/company/admin_qa/index"));
                        } else {
                            $this->error("添加失败！");
                        }
                    }
                }

    }


    public function edit(){

        $qaStatusList = CategoryService::queryQaStatusList();
        $this->assign('qa_status', $qaStatusList);

        $qaCategoryList= CategoryService::queryQaCategoryList();
        $this->assign('qa_category', $qaCategoryList);

        $memberGroupList = CategoryService::queryMemberGroupList();
        $this->assign('member_group', $memberGroupList);



        $companyModel =new CompanyModel ();


        $where = [
            'delete_time'    => 0
        ];

        $order    = "list_order";

        $companyList = $companyModel->where($where)->order($order)->select();

        $this->assign('company_list', $companyList);



        $id = $this->request->param('id', 0, 'intval');

        $qaModel = new QaModel();
        $post            = $qaModel->where('id', $id)->find();
        $this->assign('post', $post);


        return $this->fetch();
    }



    public function editPost()
    {


        if ($this->request->isPost()) {
            $data   = $this->request->param();

            $data['post']["update_time"] =  time();


            if (!empty($data['file_names']) && !empty($data['file_urls'])) {
                $data['post']['more']['files'] = [];
                foreach ($data['file_urls'] as $key => $url) {
                    $fileUrl = cmf_asset_relative_url($url);
                    array_push($data['post']['more']['files'], ["url" => $fileUrl, "name" => $data['file_names'][$key]]);
                }
            }



            if (!empty($data['post']['more']['thumbnail'])) {
                $data['post']['more']['thumbnail'] = cmf_asset_relative_url($data['post']['more']['thumbnail']);
                $data['post']['thumbnail']         = $data['post']['more']['thumbnail'];
            }


            $result = $this->validate($data['post'], 'Qa');

            if ($result !== true) {
                $this->error($result);
            } else {

                $qaModel = new QaModel ();
                $qaModel->allowField(true)->data($data['post'], true)->isUpdate(true)->save();

                if ($result !== false) {

                    $this->success("修改成功！", url("/company/admin_qa/index"));
                } else {
                    $this->error("修改失败！");
                }
            }
        }

    }


    public function reply(){

        $qaStatusList = CategoryService::queryQaStatusList();
        $this->assign('qa_status', $qaStatusList);

        $qaCategoryList= CategoryService::queryQaCategoryList();
        $this->assign('qa_category', $qaCategoryList);

        $memberGroupList = CategoryService::queryMemberGroupList();
        $this->assign('member_group', $memberGroupList);


        //echo "wxl:" . cmf_get_current_user_id();

        $companyModel =new CompanyModel ();


        $where = [
            'delete_time'    => 0
        ];

        $order    = "list_order";

        $companyList = $companyModel->where($where)->order($order)->select();

        $this->assign('company_list', $companyList);



        $id = $this->request->param('parent_id', 0, 'intval');

        $qaModel = new QaModel();
        $post            = $qaModel->where('id', $id)->find();
        $this->assign('post', $post);



        $member_id =   cmf_get_current_user_id();

        $memberModel = new MemberModel();
        $member           = $memberModel->where('id', $member_id)->find();
        $this->assign('member', $member);

        $userModel = new UserModel();
        $user            = $userModel->where('id', $member_id)->find();
        $this->assign('user', $user);




        $qaModel=   new QaModel ();
        $where = [
            'delete_time'    => 0,
            'parent_id'    => $id
        ];



        $qsList        = $qaModel->alias('a')->where($where) ->order('update_time', 'DESC')->select();

        //echo  $qaModel->getLastSql();

        $this->assign('qa_list', $qsList);



        return $this->fetch();
    }



    public function replyPost()
    {


        if ($this->request->isPost()) {
            $data   = $this->request->param();

            $data['post']["update_time"] =  time();


            if (!empty($data['file_names']) && !empty($data['file_urls'])) {
                $data['post']['more']['files'] = [];
                foreach ($data['file_urls'] as $key => $url) {
                    $fileUrl = cmf_asset_relative_url($url);
                    array_push($data['post']['more']['files'], ["url" => $fileUrl, "name" => $data['file_names'][$key]]);
                }
            }



            if (!empty($data['post']['more']['thumbnail'])) {
                $data['post']['more']['thumbnail'] = cmf_asset_relative_url($data['post']['more']['thumbnail']);
                $data['post']['thumbnail']         = $data['post']['more']['thumbnail'];
            }


            $result = $this->validate($data['post'], 'Qa');

            if ($result !== true) {
                $this->error($result);
            } else {

                $qaModel = new QaModel ();
                //修改原咨询状态
                $qaInfo = [
                    'id'    => $data['post']['parent_id'],
                    'status_id'    =>  $data['post']['status_id'],
                    'status_name'    =>  $data['post']['status_name'],

                ];
                $qaModel->allowField(true)->data($qaInfo, true)->isUpdate(true)->save();

                //新增回复意见
                $qaModel->allowField(true)->data($data['post'], true)->isUpdate(false)->save();

                if ($result !== false) {

                    $this->success("回复成功！", url("/company/admin_qa/index"));
                } else {
                    $this->error("回复失败！");
                }
            }
        }

    }



}
